/* 
 *  Copyright (C) 2009-2010 milkyjing <milkyjing@gmail.com>
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "tcx.h"

#ifdef _MSC_VER
#pragma warning(disable: 4996)
#endif    /* _MSC_VER */

#define TCX_CMD_EXIT 0
#define TCX_CMD_HANDLE_TCC 1
#define TCX_CMD_EXEC_PY 2
#define TCX_CMD_DEFAULT_TCC 3
#define TCX_CMD_DEFAULT_PY 4


int wmain(int argc, wchar_t *argv[]) {
    int len, tcxCmd;
    wchar_t szFilename[1024];
    wchar_t *outFilename;
    printf("tcx version 0.9.2 - by milkyjing\n\n");
    if (tcxpy_init_python() != py_error_success) return -1;
    if (2 == argc) {
        tcx_make_out_filename_from_tcc(argv[1], &outFilename);
        if (tcx_entry(argv[1], outFilename) != 0) {
            free(outFilename);
            printf("Task failed!\n");
            system("PAUSE");
        } else {
            free(outFilename);
            printf("Task has been done!\n");
        }
        tcxpy_fin_python();
        return 0;
    }
    while (1) {
        printf("Please enter a tcx command (0 - exit, 1 - handle tcc, 2 - execute py, 3 - create default tcc, 4 - create default py):\n");
        scanf("%i", &tcxCmd);
        if (TCX_CMD_EXIT == tcxCmd) {
            break;
        } else if (TCX_CMD_HANDLE_TCC == tcxCmd) {
            do {
                printf("Please enter the TCC filename:\n");
                wscanf(L"%s", szFilename);
                len = wcslen(szFilename);
            }
            while (!(len > 4 && _wcsicmp(szFilename + len - 4, L".tcc") == 0));
            tcx_make_out_filename_from_tcc(szFilename, &outFilename);
            if (tcx_entry(szFilename, outFilename) != 0) {
                free(outFilename);
                printf("Task failed!\n");
            } else {
                free(outFilename);
                printf("Task has been done!\n");
            }
        } else if (TCX_CMD_EXEC_PY == tcxCmd) {
            do {
                printf("Please enter the PY filename:\n");
                wscanf(L"%s", szFilename);
                len = wcslen(szFilename);
            }
            while (!(len > 3 && _wcsicmp(szFilename + len - 3, L".py") == 0));
            if (tcxpy_exec_py_script(szFilename) != tcc_error_success)
                printf("Task failed!\n");
            else
                printf("Task has been done!\n");
        } else if (TCX_CMD_DEFAULT_TCC == tcxCmd) {
            do {
                printf("Please enter the TCC filename:\n");
                wscanf(L"%s", szFilename);
                len = wcslen(szFilename);
            }
            while (!(len > 4 && _wcsicmp(szFilename + len - 4, L".tcc") == 0));
            if (libtcc_create_default_tcc_file(szFilename) != tcc_error_success)
                printf("Task failed!\n");
            else
                printf("Task has been done!\n");
        } else if (TCX_CMD_DEFAULT_PY == tcxCmd) {
            do {
                printf("Please enter the PY filename:\n");
                wscanf(L"%s", szFilename);
                len = wcslen(szFilename);
            }
            while (!(len > 3 && _wcsicmp(szFilename + len - 3, L".py") == 0));
            if (tcxpy_create_py_template(szFilename) != tcc_error_success)
                printf("Task failed!\n");
            else
                printf("Task has been done!\n");
        }
    }
    tcxpy_fin_python();
    system("PAUSE");
    return 0;
}

